package com.fmblzf.library.listview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.library.R;
import com.fmblzf.library.inter.Listener;

/**
 * Created by Administrator on 2015/11/10.
 */
public class TableListView extends LinearLayout {

    private Context mContext;//上下文

    private TextView account,relation,crowdcount,rate;

    private ExpandListView mListView;//ExpandListView

    private ImageView expand;//展开，收缩

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    private Listener listener;//监听事件

    public TableListView(Context context) {
        this(context, null, 0);
    }

    public TableListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initWidget();
    }

    /**
     * 初始化组件
     */
    private void initWidget() {
        android.view.View view = LayoutInflater.from(mContext).inflate(R.layout.table_listview_layout,null,false);
        mListView = (ExpandListView) view.findViewById(R.id.table_listview);
        account= (TextView) view.findViewById(R.id.tv_personal_account);
        relation = (TextView) view.findViewById(R.id.tv_personal_relation);
        crowdcount = (TextView) view.findViewById(R.id.tv_personal_crowd_count);
        rate = (TextView) view.findViewById(R.id.tv_personal_rate);
        expand = (ImageView) view.findViewById(R.id.iv_expand);
        expand.setOnClickListener(new ExpandOnClickListener());
        this.addView(view);
    }

    /**
     * 设置标题和格式
     */
    public void setTitles(String[] titles){
        account.setBackgroundColor(Color.parseColor("#fa9218"));
        relation.setBackgroundColor(Color.parseColor("#fa9218"));
        crowdcount.setBackgroundColor(Color.parseColor("#fa9218"));
        rate.setBackgroundColor(Color.parseColor("#fa9218"));
        if(titles == null) return;
        account.setText(titles[0]);
        relation.setText(titles[1]);
        crowdcount.setText(titles[2]);
        rate.setText(titles[3]);
    }

    /**
     * 设置适配器
     * @param adapter
     */
    public void setAdapter(ListAdapter adapter){
        mListView.setAdapter(adapter);
    }

    /**
     * 点击事件
     */
    private class ExpandOnClickListener implements android.view.View.OnClickListener{
        @Override
        public void onClick(View view) {
            if(listener!=null)
            listener.action(R.id.iv_expand,new Object[]{null});
        }
    }
}
